﻿using Consul;
using HECore.ServiceFrame.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using Refit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace HECore.ServiceFrame.Filters
{
    /// <summary>
    /// 异常处理
    /// </summary>
    public class ExceptionFilter : IAsyncExceptionFilter
    {
        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task OnExceptionAsync(ExceptionContext context)
        {
            if (context.ExceptionHandled == false)
            {
                // ApiException：异常提示
                if (context.Exception is ApiException)
                {
                    var Exception = (ApiException)context.Exception;
                    context.Result = new ContentResult
                    {
                        StatusCode = (int)Exception.StatusCode,
                        ContentType = "application/json;charset=utf-8",
                        Content = Exception.Content
                    };
                }
                // 友好异常提示：HttpStatusCode=501
                else if (context.Exception is UserFriendlyException)
                {
                    var Exception = (UserFriendlyException)context.Exception;
                    context.Result = new ContentResult
                    {
                        StatusCode = (int)HttpStatusCode.NotImplemented,
                        ContentType = "application/json;charset=utf-8",
                        Content = JsonConvert.SerializeObject(new ResultContent
                        {
                            Status = (int)HttpStatusCode.NotImplemented,
                            Code = Exception.Code,
                            Title = Exception.Title,
                            Describe = "user.friendly.exception"
                        })
                    };
                }
                // 系统异常提示：HttpStatusCode=500
                else
                {
                    context.Result = new ContentResult
                    {
                        StatusCode = (int)HttpStatusCode.InternalServerError,
                        ContentType = "application/json;charset=utf-8",
                        Content = JsonConvert.SerializeObject(new ResultContent
                        {
                            Status = (int)HttpStatusCode.InternalServerError,
                            Code = "system.error",
                            Title = "执行失败",
                            Describe = context.Exception.Message
                        })
                    };
                }
            }
            context.ExceptionHandled = true;
            return Task.CompletedTask;
        }
    }

    /// <summary>
    /// 友好异常提示：HttpStatusCode=500
    /// </summary>
    public class UserFriendlyException: Exception
    {
        /// <summary>
        /// 错误编码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 友好异常提示
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Title"></param>
        public UserFriendlyException(string Code, string Title = "")
        {
            this.Code = Code;
            this.Title = Title;
        }
    }
}
